package com.bskj.bms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bskj.bms.bean.req.ShopCouponLogListReqVo;
import com.bskj.bms.bean.req.ShopListReqVo;
import com.bskj.bms.bean.req.ShopReqVo;
import com.bskj.bms.bean.vo.ShopListVo;
import com.bskj.bms.service.IBmsCouponService;
import com.bskj.bms.service.IBmsShopService;
import com.bskj.bms.service.IBmsUserService;
import com.bskj.bms.service.IShopCouponLogService;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.CodeValue;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.bean.model.ShopCoupon;
import com.bskj.common.bean.model.ShopCouponLog;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.Page;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.BmsUserTypeEnum;
import com.bskj.common.utils.StringUtils;

@Controller
@RequestMapping("/shop")
public class ShopController extends BaseController {
	@Autowired
	private IBmsShopService bmsShopService;

	@Autowired
	private IBmsUserService bmsUserService;

	@Autowired
	private IBmsCouponService bmsCouponService;

	@Autowired
	private IShopCouponLogService shopCouponLogService;

	@RequestMapping("/index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/shop/indexs");
		return mv;
	}

	@RequestMapping("/toDelete")
	public ModelAndView toDelete(@RequestParam(required = false) Integer shopId) throws Exception {
		ParkingShop parkingShop = bmsShopService.get(shopId);
		ModelAndView mv = new ModelAndView();
		mv.addObject("parkingShop", parkingShop);
		mv.setViewName("/shop/deleteShop");
		return mv;
	}

	@RequestMapping("/toInstall")
	public ModelAndView toInstall(@RequestParam(required = false) Integer id) {
		ShopCoupon shopCoupon = bmsCouponService.get(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("shopCoupon", shopCoupon);
		mv.setViewName("/coupon/installCoupon");
		return mv;
	}

	@ResponseBody
	@RequestMapping("install")
	public Map<String, Object> install(ShopCoupon shopCoupon) throws Exception {
		BmsUser user = getSessionUser();
		Map<String, Object> resultMap = bmsCouponService.setShopCouponLimit(shopCoupon, user);
		return resultMap;
	}

	@RequestMapping("/openSaveOrUpdate")
	public ModelAndView openAddShop(@RequestParam(required = false) Integer shopId) throws Exception {
		ModelAndView mv = new ModelAndView();
		if (shopId != null) {
			ParkingShop parkingShop = bmsShopService.findShopData(shopId);
			mv.addObject("parkingShop", parkingShop);
		}
		mv.setViewName("/shop/saveOrUpdate");
		List<CodeValue> list = bmsShopService.findShopType();
		mv.addObject("shopType", list);
		return mv;
	}

	@RequestMapping("/shopData")
	public ModelAndView shopData(@RequestParam(required = false) Integer shopId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/shop/shopData");
		ParkingShop parkingShop = bmsShopService.findShopData(shopId);
		mv.addObject("parkingShop", parkingShop);
		return mv;
	}

	@RequestMapping("addShop")
	@ResponseBody
	public Map<String, Object> addShop(ShopReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		req.setCustId(getSessionUser().getCustId());
		if (req.getId() != null) {
			resultMap = bmsShopService.updateShop(req);

		} else {
			resultMap = bmsShopService.addShop(req);
		}
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("/list")
	public Map<String, Object> list(ShopListReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		req.setCustId(getSessionUser().getCustId());
		Page<ParkingShop> parkingShops = bmsShopService.findAllShop(req);
		List<ShopListVo> shopListVos = new ArrayList<>();
		List<ParkingShop> parkingShopList = parkingShops.getList();
		for (ParkingShop parkingShop : parkingShopList) {

			ShopListVo shopListVo = new ShopListVo();
			FindRequestEntity findReq = new FindRequestEntity();
			findReq.addWhere("shop_id", WhereEnum.EQ, parkingShop.getId());
			List<ShopCoupon> shopCoupons = bmsCouponService.find(findReq);
			shopListVo = shopListVo.getShopListVo(parkingShop, shopCoupons);
			shopListVos.add(shopListVo);
		}
		resultMap.put("data", shopListVos);
		resultMap.put("draw", req.getDraw());
		resultMap.put("recordsTotal", shopListVos.size());
		resultMap.put("recordsFiltered", parkingShops.getTotalRow());
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("/delete")
	@Transactional(rollbackFor = Exception.class)
	public Map<String, Object> delete(@RequestParam(required = false) Integer shopId) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			ParkingShop parkingShop = bmsShopService.get(shopId);
			BmsUser bmsUser = bmsUserService.findUser(parkingShop.getPhone(), parkingShop.getPhone());
			bmsUserService.del(bmsUser.getId());
			bmsShopService.del(shopId);
			bmsShopService.delRule(bmsUser.getId());
			resultMap.put("success", true);
			resultMap.put("meg", "删除成功！");
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("meg", "系统正忙，修改失败！");
			e.printStackTrace();
			return resultMap;
		}
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("changeStatus")
	public Map<String, Object> list(Integer id, int status) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		ParkingShop shop = bmsShopService.get(id);
		if (shop == null) {
			resultMap.put("success", false);
			resultMap.put("msg", "商店不存在");
			return resultMap;
		} else {
			if (BmsUserTypeEnum.get(status) == null) {
				resultMap.put("success", false);
				resultMap.put("msg", "请求参数不合法");
				return resultMap;
			}
			shop.setStatus(status);
			shop.setUpdateTime(new Date());
			bmsShopService.update(shop);
			resultMap.put("success", true);
		}
		return resultMap;
	}

	@RequestMapping("/shopCouponLog/index")
	public ModelAndView shopCouponLogIndex() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/shop/shopCouponLog/index");
		return mv;
	}

	@ResponseBody
	@RequestMapping("/shopCouponLog/list")
	public Map<String, Object> shopCouponLogList(ShopCouponLogListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		FindRequestEntity findReq = new FindRequestEntity();
		if (bmsUser != null && BmsUserTypeEnum.parking_lot_user.getKey() == bmsUser.getType()) {
			findReq.addWhere("parking_lot_customer_id", WhereEnum.EQ, bmsUser.getCustId());
		}

		if (reqVo.getStartTime() != null) {
			findReq.addWhere("create_time", WhereEnum.GE, reqVo.getStartTime());
		}

		if (reqVo.getEndTime() != null) {
			findReq.addWhere("create_time", WhereEnum.LE, reqVo.getEndTime());
		}

		if (!StringUtils.isBank(reqVo.getOperator())) {
			findReq.addWhere("operator", WhereEnum.LIKE, "%" + reqVo.getOperator() + "%");
		}

		if (!StringUtils.isBank(reqVo.getShopName())) {
			findReq.addWhere("shop_name", WhereEnum.LIKE, "%" + reqVo.getShopName() + "%");
		}
		findReq.addOrder("create_time", OrderEnum.DESC);
		findReq.setPage(reqVo.getPageNo(), reqVo.getPageSize());

		Page<ShopCouponLog> resultPage = shopCouponLogService.page(findReq);

		resultMap.put("data", resultPage.getList());
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", resultPage.getList().size());
		resultMap.put("recordsFiltered", resultPage.getTotalRow());
		
		return resultMap;
	}
}
